import os
import yaml
import wx
import wx.xrc as xrc

class YavfApp(wx.App):
	def OnInit(self):
		self.res = xrc.XmlResource("yavf.xrc")
		self.InitFrame()
		self.InitFinalize()
		return True

	def InitFrame(self):
		self.frame = self.res.LoadFrame(None, "mainFrame")
		self.tree = xrc.XRCCTRL(self.frame, "vfList")
		wx.EVT_TOOL(self, xrc.XRCID("exitTool"), self.OnQuit)
		wx.EVT_TOOL(self, xrc.XRCID("openTool"), self.OnOpen)

	def InitFinalize(self):
		self.frame.Show()
	
	def OnQuit(self, event):
		#FIXME check if saved
		self.Exit()

	def OnOpen(self, event):
		wildcard = "Virtual Folder File (*.vff)|*.vff"
		dialog = wx.FileDialog(None, "Choose a .vff file", os.getcwd(), "", wildcard, wx.OPEN)
		if dialog.ShowModal() == wx.ID_OK:
			vff = dialog.GetPath()
		dialog.Destroy()

		if vff:
			data = yaml.load(file(vff, "r").read())
			print data

			self.AddTreeNodes(None, data)
			#self.tree.Expand(self.tree.GetRootItem())
			self.tree.ExpandAll()

	def AddTreeNodes(self, parent, items):
		for i in items:
			if type(items[i]) == str:
				if parent == None:
					root = self.tree.AddRoot(i)
				else:
					self.tree.AppendItem(parent, i)
			else:
				if parent == None:
					root = self.tree.AddRoot(i)
					self.AddTreeNodes(root, items[i])
				else:
					newItem = self.tree.AppendItem(parent, i)
					self.AddTreeNodes(newItem, items[i])

def main():
	app = YavfApp(0)
	app.MainLoop()

if __name__ == '__main__':
	main()

